<template>
  <div class="teachMaterialDBManage">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教材管理</el-breadcrumb-item>
      <el-breadcrumb-item>教材库信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="20" class="mb-12">
      <el-col :span="6">
        <el-input v-model="searchForm.courseNameCN" placeholder="请输入课程名称"></el-input>
      </el-col>
      <el-col :span="6">
        <el-input v-model="searchForm.textbookName" placeholder="请输入教材名称"></el-input>
      </el-col>
      <el-col :span="6" class="overflow-clear">
        <el-button type="primary" @click="submitForm" class="float-left">立即搜索</el-button>
      </el-col>
    </el-row>
    <hr>
    <div class="overflow-clear">
      <!--<div class="float-left table-title-before">
        教材库管理
      </div>-->
      <el-button-group class="float-right mb-5">
        <router-link to="/NewTeachMaterial" tag="el-button" class="el-button--primary">新增</router-link>
        <!--<el-button type="primary">打印</el-button>-->
      </el-button-group>
    </div>
    <el-table
      :data="textbookDBTable"
      style="width: 100%">
      <el-table-column
        prop="courseNameCN"
        label="课程名称"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="textbookId"
        label="教材编号"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="textbookName"
        label="教材名称"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="publishingHouse"
        label="出版社"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="isbn"
        label="ISBN"
        min-width="140">
      </el-table-column>
      <el-table-column
        prop="price"
        label="单价"
        min-width="100">
      </el-table-column>

      <el-table-column
        label="操作"
        min-width="120">
        <template scope="scope">
          <el-button icon="edit" type="text" @click="updateTeachMaterial(scope.row.textbookId)"></el-button>
          <el-button icon="delete" type="text"
                     @click="deleteRow(scope.$index, textbookDBTable,scope.row.textbookId)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="paging">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<style>
</style>
<script>
  export default{
    name: 'teachMaterialDBManage',
    data(){
      return {
        textbookDBTable: [],
        searchForm: {
          courseNameCN: '',
          textbookName: ''
        },
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 30, 40],
        total: 40,
      }
    },
    components: {},
    mounted: function () {
      this.$nextTick(function () {
        this.getTextBookRes();
      })
    },
    methods: {
      getTextBookRes: function () {
        this.$http.post(this.$until.url + '/textBookManage/getTextBook', this.searchForm,
          {
            params: {
              page: this.currentPage,
              pageSize: this.pageSize
            },
            emulateJSON: true
          }).then(response => {
          this.total = response.body.countNum;
          this.textbookDBTable = response.body.textBookList
        }, response => {
          alert("no file page");
        });
      },
      submitForm() {
        this.getTextBookRes();
      },
      updateTeachMaterial(textbookId){
        this.$router.push({name: 'TeachMaterialFix', params: {textbookId: textbookId}})
      },
      deleteRow: function (index, rows, textbookId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRes(textbookId);
          rows.splice(index, 1);
          this.total = this.total - 1;
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      deleteRes: function (textbookId) {
        this.$http.get(this.$until.url + '/textBookManage/deleteTextBook',
          {
            params: {
              textbookId: textbookId
            }
          }).then(response => {
          this.$message({
            type: 'success',
            message: response.body.message
          });
        }, response => {
          alert("找不到delete文件");
        });
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.getTextBookRes();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getTextBookRes();
      }
    }
  }
</script>
